/*
 * @Description: 
 * @Author: mmzhang
 * @Date: 2021-11-15 14:38:47
 * @LastEditTime: 2022-12-15 15:51:30
 */
import userApi from '@/config/server/api/user.js'
import marketingApi from '@/config/server/api/marketing.js'

function getDateList(info) {
  return userApi.dateList.fetch(info).then(res => {
    let resolveArr = multistageArr(res)
    return resolveArr
  })
}
// 递归处理
function multistageArr(arr) {
  let newArr = []
  arr.forEach(item => {
    item.label = item.desc
    item.value = item.number
    if (item.childList && item.childList.length) {
      item.children = item.childList
      multistageArr(item.children); //递归遍历
    }
    newArr.push(item)
  })
  return newArr
}

function getSettingInfo(info) {
  return marketingApi.promote.settingInfo.fetch(info).then(res => {
    let resolveData = multistageSetArr(res)
    return resolveData
  })
}

function multistageSetArr(arr) {
  let dateOptions = []
  let containDate = []
  arr.forEach(item => {
    let id = item.contentType || item.promoteMethodId
    if (item.checked) {
      containDate.push(id)
    }
    item.key = id
    item.check = item.checked
    item.showText = item.title
    item.checkChild = []


    if (item.promoteMethodIds && item.promoteMethodIds.length) {
      item.promoteMethodIds.forEach(childItem => {
        if (childItem.checked) {
          item.checkChild.push(childItem.promoteMethodId)
        }
      })
      item.children = item.promoteMethodIds
      multistageSetArr(item.children); //递归遍历
    }
    dateOptions.push(item)
  })
  return {
    dateOptions: dateOptions,
    containDate: containDate
  }
}

function settingFun(info) {
  return marketingApi.promote.setting.fetch(info).then(res => {
    return res
  })
}

function getPromoteExecuteList(params) {
  return marketingApi.promote.getPromoteExecuteList.fetch(params).then(res => {
    return res
  })
}

function getPromoteExecuteCount(params) {
  return marketingApi.promote.getPromoteExecuteCount.fetch(params).then(res => {
    return res
  })
}

function getPromoteExecuteExport(params) {
  return marketingApi.promote.getPromoteExecuteExport.fetch(params).then(res => {
    return res
  })
}

function getRewardShareRoleList(info) {
  return userApi.role.rewardShareRoleList.fetch(info).then(res => {
    res.forEach(ele => {
      ele.key = ele.roleId
      ele.value = ele.roleName
    });
    return res
  })
}

// 推广记录
function getBuildingOrgCustomized(params) {
  return userApi.getBuildingOrgCustomized.fetch(params).then(res => {
    return res
  })
}

function getPromoteList(params) {
  return marketingApi.promote.promoteList.fetch(params).then(res => {
    return res
  })
}

function getPromoteCount(params) {
  return marketingApi.promote.promoteCount.fetch(params).then(res => {
    return res
  })
}

function promoteExportFun(params) {
  return marketingApi.promote.promoteExport.fetch(params).then(res => {
    return res
  })
}

function importArticleDeleteIdsFun(params) {
  return marketingApi.importArticle.importArticleDeleteIds.fetch(params).then(res => {
    return res
  })
}


export default {
  getDateList,
  getSettingInfo,
  settingFun,
  getPromoteExecuteList,
  getPromoteExecuteCount,
  getPromoteExecuteExport,
  getBuildingOrgCustomized,
  getRewardShareRoleList,
  getPromoteList,
  getPromoteCount,
  promoteExportFun,
  importArticleDeleteIdsFun
}